package dao;

import model.Manager;
import util.ArgsList;
import util.JDBCConnection;
import util.JDBCFactory;
import util.ResultList;

import java.sql.SQLException;
import java.util.List;

public class ManagersManager implements ManagersManagement {
    @Override
    public Integer SaveOne(Manager manager, UsrTypeManager typeManager) {
        if(typeManager == null) return null;
        if(manager == null  ||  manager.getU_level() == null  ||  manager.getU_id() == null) return null;

        Integer level = manager.getU_level();
        try {
            JDBCConnection conn = JDBCFactory.takeJDBC();
            conn.start();
            if (conn.update("insert into t_managers values(?,?)", manager.getU_id(), manager.getU_level()) != 1)
                level = null;
            if (conn.update("insert into t_usertype values(?,?)", manager.getU_id(), typeManager.getUserType(conn, "平台管理员", "平台管理员用户")) != 1)
                level = null;

            conn.commit();
            JDBCFactory.returnBack(conn);

            return level;
        } catch(SQLException e) {
            System.err.println(e.getMessage());
            return null;
        }
    }

    @Override
    public Integer updateOne(long managerID, int level) {
        Integer levl = level;
        try {
            JDBCConnection conn = JDBCFactory.takeJDBC();
            if (conn.update("update t_managers set u_level = ? where u_id = ?", level, managerID) != 1) levl = null;
            JDBCFactory.returnBack(conn);

            return levl;
        } catch(SQLException e) {
            System.err.println(e.getMessage());
            return null;
        }
    }

    @Override
    public Integer deleteOne(long u_id, UsrTypeManager typeManager) {
        try {
            JDBCConnection conn = JDBCFactory.takeJDBC();
            conn.start();
            Integer count = conn.update("delete from t_usertype where u_id = ? and utyp_id = ?", u_id, typeManager.getUserType(conn, "平台管理员", "平台管理员用户"));
            if (conn.update("delete from t_managers where u_id = ?", u_id) != 1) count = null;
            conn.commit();
            JDBCFactory.returnBack(conn);
            return count;
        } catch(SQLException e) {
            System.err.println(e.getMessage());
            return null;
        }
    }

    @Override
    public List<Manager> query(Manager manager, int pageSize, int page) {
        boolean firstCondition = true;

        StringBuilder sql = new StringBuilder("select * from t_managers");
        ArgsList<Object> arg_whereCondition = new ArgsList<>();

        //构造条件
        if(manager != null)
        {
            if(manager.getU_id() != null)
            {
                sql.append(" where u_id = ?");
                arg_whereCondition.add(manager.getU_id());
                firstCondition = false;
            }

            if(manager.getU_level() != null)
            {
                if(!firstCondition) sql.append(" and");
                sql.append(" where u_level = ?");
                arg_whereCondition.add(manager.getU_level());
            }
        }

        if(pageSize > 0  &&  page > 0)
        {
            sql.append(" limit ?, ?");
            arg_whereCondition.add((Integer)((page - 1) * pageSize));
            arg_whereCondition.add((Integer)pageSize);
        }
        try {
            JDBCConnection conn = JDBCFactory.takeJDBC();
            List<Manager> result = getResultList(conn.query(arg_whereCondition, sql.toString()));
            JDBCFactory.returnBack(conn);

            return result;
        } catch(SQLException e) {
            System.err.println(e.getMessage());
            return null;
        }
    }

    @Override
    public List<Manager> query(Manager man) {
        return query(man, 0, 0);
    }
    @Override
    public List<Manager> query(int pageSize, int page) {
        return query(null, pageSize, page);
    }
    @Override
    public List<Manager> query() {
        return query(null);
    }
    @Override
    public List<Manager> queryALL() {
        return query();
    }
    @Override
    public List<Manager> queryALL(int pageSize, int page) {
        return query(pageSize, page);
    }

    /**
     * 包装并返回查询到的对象的列表
     * @param list 包含 SQL 行查询结果的列表，Object[]中每一项都是一列的值
     * @return 返回包装好的对象列表
     */
    private static List<Manager> getResultList(List<Object[]> list)
    {
        Manager man = null;

        List<Manager> result = new ResultList<>();

        for(Object[] row : list)
        {
            man = new Manager();

            man.setU_id((Long) row[0]);
            man.setU_level((Integer) row[1]);

            result.add(man);
        }
        return result;
    }
}
